//
//  HMHomeCellTableViewCell.swift
//  GuoGuo
//
//  Created by 艾小新 on 16/8/8.
//  Copyright © 2016年 xiaoxin. All rights reserved.
//  自定义cell

import UIKit
import SnapKit

class HMHomeCellTableViewCell: UITableViewCell {
    
    
    var middleTopCon:Constraint?
    
    var statusViewModel: HMCellViewModel? {
    
        didSet {
            // 在这里进行赋值
            self.topView.statusViewModel = statusViewModel
            self.bottomView.statusViewModel = statusViewModel
            
            // 如果有转发微博就显示middleView界面
            if let _ = statusViewModel?.status?.retweeted_status  {
                
                // 显示界面，改变约束，赋值
                self.middleView.hidden = false
                
                middleTopCon?.uninstall()
                
                bottomView.snp_makeConstraints(closure: { (make) in
                    middleTopCon = make.top.equalTo(middleView.snp_bottom).constraint
                })
                
                // 赋值
                self.middleView.statusViewModel = statusViewModel
                
            } else {
                
                // 隐藏界面，改变约束
                self.middleView.hidden = true
                
                middleTopCon?.uninstall()
                
                middleTopCon?.uninstall()
                
                bottomView.snp_makeConstraints(closure: { (make) in
                    middleTopCon = make.top.equalTo(topView.snp_bottom).constraint
                })

            }
            
        }
    
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        selectionStyle = .None
        // 添加视图
        contentView.addSubview(topView)
        contentView.addSubview(bottomView)
        contentView.addSubview(middleView)
    
        // 约束
        topView.snp_makeConstraints { (make) in
            // 上左右都靠着contentView，高度是200
            make.trailing.leading.top.equalTo(contentView)
        }
        
        
        middleView.snp_makeConstraints { (make) in
            make.trailing.leading.equalTo(topView)
            make.top.equalTo(topView.snp_bottom)
            
        }
        
        
        bottomView.snp_makeConstraints { (make) in
            make.height.equalTo(35)
            make.leading.trailing.equalTo(topView)
            middleTopCon = make.top.equalTo(middleView.snp_bottom).constraint
        }
        
        contentView.snp_makeConstraints { (make) in
            make.top.trailing.leading.equalTo(self)
            make.bottom.equalTo(bottomView)
        }
    }
    
    // MARK: -懒加载
    
    private lazy var topView: HMTopView = HMTopView()
    
    private lazy var bottomView: HMBottomView = HMBottomView()
    
    private lazy var middleView: HMMiddleView = HMMiddleView()
    
    
    

    

}
